<?php
/**
 * Parse requested address and show particular tab
 * 
 * @author Aurelijus Banelis
 */

$started = microtime(true);

/* Loading common libraries */
require 'common.php';
include 'database.php';
include 'GUI/gui_functions.php';
//for Backward compatibility
include 'GUI/design.php';
db_connect();

$help_links = Array();
$sidebar_content = "";

/* Form data processing */
//FIXME: security
if(isset($_POST['real_action_script']) && $_POST['real_action_script'] == 'ApplicationQuantitiesImport.php'){
    ob_start();
    include_once($_POST['real_action_script']);
    //FIXME: wen importing data: "failed to delete buffer. No buffer to delete"
    @ob_clean();
}

/* Parsing address */
$requested_tab = Addresing::getRequestedTab();

/* List of tabs */
$tabs_array = array(
    'input' => 'enterDataTab.php|Importas',
    'database' => 'viewDatabase.php|Esami duomenys',
    'subdivisions' => 'subdivisionLoadTab.php|Apkrovos peržiūra',
//    'load' => 'changesInfluenceTab.php|Pakeitimų įvedimas',
//    'changes' => 'loadForeseeTab.php|Min apkrovos radimas'
);

/* Default command */
if (!in_array($requested_tab, array_keys($tabs_array))) {
    $requested_tab = array_search(current($tabs_array), $tabs_array);
}

/* Current command */
list($file, $page_title) = explode('|', $tabs_array[$requested_tab]);

/* Creating tabs list */
$tabs = "";
foreach ($tabs_array as $tab_link=>$info) {
    list($tabFile, $name) = explode('|', $info, 2);
    $tabs .= tabHeader($name, "?$tab_link", $requested_tab == $tab_link);
}

ob_start();
include "GUI/$file";
$main_content = ob_get_clean();

$help = "";
foreach ($help_links as $link){
    $help .= form_help_link($link);
}


/* Finalising */
db_close();
$time = round((microtime(true) - $started), 2);
$footer = "Užtruko $time s.";
if (function_exists('memory_get_peak_usage')) {
    $ram = round(memory_get_peak_usage() / 1024, 2);
    $footer .= "\nAtminties suvartojo: ";
    if ($ram < 1024) {
        $footer .= "$ram kB";
    } else {
        $ram = round($ram / 1024, 2);        
        $footer .= "$ram MB";
    }
}

include 'GUI/design2.php';